day 23 init

This commit is contained in:
2022-01-28 19:37:23 +01:00
parent 5c392199fe
commit 1e8556b5d5
6 changed files with 225 additions and 14 deletions

View File

@@ -253,3 +253,27 @@ aoc-c : res=5475
aoc-c : res=17548
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 1+1, page-faults: 0+108
=========================================
================= day21 =================
=========================================
aoc-c : res=989352
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+84
aoc-c : res=430229563871565
time: 0:03.11 real, 2.99 user, 0.11 sys
context-switch: 78+1, page-faults: 0+32804
=========================================
================= day22 =================
=========================================
aoc-c : res=606484
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 3+1, page-faults: 0+337
aoc-c : res=1162571910364852
time: 0:00.19 real, 0.18 user, 0.00 sys
context-switch: 17+1, page-faults: 0+973

View File

@@ -20,8 +20,8 @@
#include "bits.h"
#include "list.h"
typedef struct step {
int sign; /* 0: negative */
typedef struct {
int sign; /* 0: negative, 1: positive */
int x[2], y[2], z[2]; /* look for 'Arghh' below ;-) */
struct list_head list_step;
} step_t;
@@ -31,8 +31,8 @@ LIST_HEAD(list_step);
pool_t *pool_step;
int ncubes;
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
static inline s64 cube_volume(step_t *c)
{
@@ -104,8 +104,8 @@ static inline step_t *cube_intersect(step_t *old, step_t *new)
return cube;
}
/* brute force approach, I did not test with part 2 algorithm,
* my guess is that it could be worse...
/* Part 1: Brute force approach, I did not test with part 2 algorithm,
* my guess is that it could be much slower...
*/
static s64 part1()
{
@@ -146,7 +146,7 @@ static s64 part1()
*/
static s64 part2()
{
step_t *cur, *tmp, *new, *inter;
step_t *cur, *tmp, *new, *intersect;
s64 res = 0;
pool_step = pool_create("steps", 2048, sizeof(step_t));
@@ -154,13 +154,12 @@ static s64 part2()
while ((new = read_instruction(tmp = pool_get(pool_step)))) {
LIST_HEAD(list_tmp); /* temp intersections list */
list_for_each_entry(cur, &list_step, list_step) {
/* intersection found: we insert it to negate cur
* ones.
/* intersection found: we insert it to negate cur ones.
*/
if ((inter = cube_intersect(cur, new))) {
inter->sign = !cur->sign; /* negates intersection */
list_add_tail(&inter->list_step, &list_tmp);
res += cube_volume(inter);
if ((intersect = cube_intersect(cur, new))) {
intersect->sign = !cur->sign; /* negates intersection */
list_add_tail(&intersect->list_step, &list_tmp);
res += cube_volume(intersect);
}
}
/* add temp intersections list to global's tail
@@ -175,7 +174,7 @@ static s64 part2()
pool_add(pool_step, tmp); /* release memory */
}
}
pool_destroy(pool_step);
pool_destroy(pool_step); /* please valgrind */
return res;
}

5
2021/day23/EXAMPLE.txt Normal file
View File

@@ -0,0 +1,5 @@
#############
#...........#
###B#C#B#D###
#A#D#C#A#
#########

5
2021/day23/INPUT.txt Normal file
View File

@@ -0,0 +1,5 @@
#############
#...........#
###B#C#A#D###
#B#C#D#A#
#########

77
2021/day23/Makefile Normal file
View File

@@ -0,0 +1,77 @@
# AOC daily Makefile - GNU make only.
#
# Copyright (C) 2021 Bruno Raoult ("br")
# Licensed under the GNU General Public License v3.0 or later.
# Some rights reserved. See COPYING.
#
# You should have received a copy of the GNU General Public License along with this
# program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
#
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
#
INPUT := INPUT.txt
SHELL := /bin/bash
CC := gcc
LIB := aoc_$(shell uname -m)
INCDIR := ../include
LIBDIR := ../lib
LDFLAGS := -L$(LIBDIR)
#LDLIB := -l$(LIB) -lm
LDLIB := -l$(LIB)
export LD_LIBRARY_PATH = $(LIBDIR)
CFLAGS += -std=gnu99
CFLAGS += -O2
CFLAGS += -g
# for gprof
#CFLAGS += -pg
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -march=native
# Next one may be useful for valgrind (some invalid instructions)
# CFLAGS += -mno-tbm
CFLAGS += -Wmissing-declarations
CFLAGS += -Wno-unused-result
CFLAGS += -DDEBUG_DEBUG # activate general debug (debug.c)
CFLAGS += -DDEBUG_POOL # memory pools management
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
export PATH := .:$(PATH)
.PHONY: clean all compile assembly memcheck memcheck1 memcheck2 ex1 ex2
all: ex1 ex2
memcheck: memcheck1 memcheck2
memcheck1:
@valgrind -q -s --track-origins=yes aoc-c -p 1 < $(INPUT)
memcheck2:
@valgrind -q -s --track-origins=yes aoc-c -p 2 < $(INPUT)
compile: aoc-c
assembly: aoc-c.s
ex1: aoc-c
@$(TIME) aoc-c -p 1 < $(INPUT)
ex2: aoc-c
@$(TIME) aoc-c -p 2 < $(INPUT)
clean:
@rm -f aoc-c core* vgcore* gmon.out aoc-c.s
.c:
@echo compiling $<
@$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $< $(LDLIB) -o $@
.c.s:
@echo generating $@
@$(CC) -S -fverbose-asm $(CFLAGS) -I $(INCDIR) $< -o $@

101
2021/day23/README.txt Normal file
View File

@@ -0,0 +1,101 @@
--- Day 23: Amphipod ---
A group of amphipods notice your fancy submarine and flag you down. "With such an impressive shell," one amphipod says, "surely you can help us with a question that has stumped our best scientists."
They go on to explain that a group of timid, stubborn amphipods live in a nearby burrow. Four types of amphipods live there: Amber (A), Bronze (B), Copper (C), and Desert (D). They live in a burrow that consists of a hallway and four side rooms. The side rooms are initially full of amphipods, and the hallway is initially empty.
They give you a diagram of the situation (your puzzle input), including locations of each amphipod (A, B, C, or D, each of which is occupying an otherwise open space), walls (#), and open space (.).
For example:
#############
#...........#
###B#C#B#D###
#A#D#C#A#
#########
The amphipods would like a method to organize every amphipod into side rooms so that each side room contains one type of amphipod and the types are sorted A-D going left to right, like this:
#############
#...........#
###A#B#C#D###
#A#B#C#D#
#########
Amphipods can move up, down, left, or right so long as they are moving into an unoccupied open space. Each type of amphipod requires a different amount of energy to move one step: Amber amphipods require 1 energy per step, Bronze amphipods require 10 energy, Copper amphipods require 100, and Desert ones require 1000. The amphipods would like you to find a way to organize the amphipods that requires the least total energy.
However, because they are timid and stubborn, the amphipods have some extra rules:
Amphipods will never stop on the space immediately outside any room. They can move into that space so long as they immediately continue moving. (Specifically, this refers to the four open spaces in the hallway that are directly above an amphipod starting position.)
Amphipods will never move from the hallway into a room unless that room is their destination room and that room contains no amphipods which do not also have that room as their own destination. If an amphipod's starting room is not its destination room, it can stay in that room until it leaves the room. (For example, an Amber amphipod will not move from the hallway into the right three rooms, and will only move into the leftmost room if that room is empty or if it only contains other Amber amphipods.)
Once an amphipod stops moving in the hallway, it will stay in that spot until it can move into a room. (That is, once any amphipod starts moving, any other amphipods currently in the hallway are locked in place and will not move again until they can move fully into a room.)
In the above example, the amphipods can be organized using a minimum of 12521 energy. One way to do this is shown below.
Starting configuration:
#############
#...........#
###B#C#B#D###
#A#D#C#A#
#########
One Bronze amphipod moves into the hallway, taking 4 steps and using 40 energy:
#############
#...B.......#
###B#C#.#D###
#A#D#C#A#
#########
The only Copper amphipod not in its side room moves there, taking 4 steps and using 400 energy:
#############
#...B.......#
###B#.#C#D###
#A#D#C#A#
#########
A Desert amphipod moves out of the way, taking 3 steps and using 3000 energy, and then the Bronze amphipod takes its place, taking 3 steps and using 30 energy:
#############
#.....D.....#
###B#.#C#D###
#A#B#C#A#
#########
The leftmost Bronze amphipod moves to its room using 40 energy:
#############
#.....D.....#
###.#B#C#D###
#A#B#C#A#
#########
Both amphipods in the rightmost room move into the hallway, using 2003 energy in total:
#############
#.....D.D.A.#
###.#B#C#.###
#A#B#C#.#
#########
Both Desert amphipods move into the rightmost room using 7000 energy:
#############
#.........A.#
###.#B#C#D###
#A#B#C#D#
#########
Finally, the last Amber amphipod moves into its room, using 8 energy:
#############
#...........#
###A#B#C#D###
#A#B#C#D#
#########
What is the least energy required to organize the amphipods?
To begin, get your puzzle input.