From 77250a25680bbf2d01c928290f0dcd119166841a Mon Sep 17 00:00:00 2001 From: plasmaofthedawn Date: Sat, 23 Dec 2023 20:27:55 -0500 Subject: [PATCH] day 16 --- Makefile | 11 ++ readme.md | 3 + resources/day16.txt | 110 +++++++++++++++++++ resources/day16sample.txt | 10 ++ src/day16/part1.pas | 146 +++++++++++++++++++++++++ src/day16/part2.pas | 219 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 499 insertions(+) create mode 100644 resources/day16.txt create mode 100644 resources/day16sample.txt create mode 100644 src/day16/part1.pas create mode 100644 src/day16/part2.pas diff --git a/Makefile b/Makefile index e35df5c..5a99a5c 100644 --- a/Makefile +++ b/Makefile @@ -154,5 +154,16 @@ day15part2: @echo ./build/day15part2 +day16part1: + fpc src/day16/part1.pas -obuild/day16part1 + @echo + ./build/day16part1 + +day16part2: + fpc src/day16/part2.pas -obuild/day16part2 + @echo + ./build/day16part2 + + clean: rm build/* \ No newline at end of file diff --git a/readme.md b/readme.md index 0ec00cf..9cab073 100644 --- a/readme.md +++ b/readme.md @@ -39,4 +39,7 @@ it uses zasm to compile and z80sim from [z80pack](https://www.autometer.de/unix4 i edited z80sim to not print out that giant logo at the beginning but otherwise it's just the newest one built. the binary is included though lol. +make day13part1z80 i think is the make for it. +output is in the hl register. + diff --git a/resources/day16.txt b/resources/day16.txt new file mode 100644 index 0000000..5ef6ce0 --- /dev/null +++ b/resources/day16.txt @@ -0,0 +1,110 @@ +\.........|......./...|........................................................|............|-...\...-/....... +.....................\.......\........./...|...............|...\................................-...|.......\. +.......................................-....-./............/.......-.............\..|.......|......//......... +...../..../.....\.........-.........-./..-.......\./..\.............-.....\..........|-..................|.... +...........\..\...............-.............|.......-.\......\...\..........\.......-.....|..................\ +................|.........|........|............--........./\....|-.....................................-..... +........./...........\................../...................\././........./...-...-.\.-.........-............. +......|.....|............-............./...|...................-...|........./............/......./........... +......./........../..|.....-...........\|.................................................\.........|..../.... +........................|..................|.......|.................-..\...........-/.\.................\.|.. +.......|...|./....................................-...\........\.......|............................|...|..... +\..-................................\.......\........../....|.....................|........\../.........../..\ +....-|...............................\....|..............\.....................|.....\......\............../.. +.....\......-.........-.\.|..........|...............-.................|..........-............../............ +.........-...................-.............................................-.........../...................|.- +....................\..\........|-.\...............-............/....|..................../................... +.\.........-........................|..-.......|.......-...........-.-...................\/...\....|....\..... +.-........-./.....-.....\.......-..........\..-........\..-.....................................\..../...|.... +....-.........................|..\.....................................\./...............-............/....... +..........|..\....-........./..............................|........................-.......|./|........\..... +..\.........|..|..-|........\...-................|/..\......../.......-|..................||.-./.............. +.--..-.......-........./.................\....../.................-............................--.........-... +.......|.........../......./........../..\.........\...\../..\..././.......-.................................. +.........-/...............|..\|.................\...........|............../............\..................... +..........-.........\.-................|-..................../......../...|/...\..-....|-........-.......-.... +.--|.\|.......|.....-............................|.-....-...|...................|........../.................. +.....-......./..../.|...................|\..-.........../...|.............................\/../........../.... +......./........|.\......-../...........\........................./..........\.........-...\................|. +./.......................\........-......|...|............................/............\....-.....|..../...\.. +....................-..............-..-.............../..\...../.\.........\................../.....\.....-... +...................../......-....-.......|....|......../..|-...............\..\../.-.....././..........|...... +..\.....|...\............................./.../...../..../......../........................................... +./.../................................../.....-.........|.....|..........-...............................-.... +...............|..\...-................................\.........................|....\...............-.-..... +/........|.......|.....-.......-..-..-.\.................................-.\...-.......\.............-..\..... +./......|../......|................................/.........||.-....................|..\.........-....|../... +|..................................................\......|...-............................../.......-........ +....../...............-....-..................\....-../................................./.\.................-. +......-...........-...../.....-......|..........\.\...-......../......\................--..............-...... +.............\/........|...../...........-.......\...|........................................................ +...\\........\..........|...\............/........-..../....-.....................-......................../.. +.......-......../......./..........-....|....-..\.........................\.............../..........-.....-.. +.....|\/..../.........|.........../|.....\.......-........-............../..././.............|................ +.........|....|....\..-....../............/.....\/....|.............\........................|.........-...... +.\\..........................//......................\....\...........................-.|/.................... +...................|...................-.|........................................../..........\.............. +......\..|....../...........\.-.........-.......-..|../........../.............................../.......-...| +..........|................/|.....-.....\................||........./.............|.........../..-..|./....... +............../../|.......\...............-..../|..../-..|................................../......-....|..|.. +............\...............\.........................\....-.............-..|/.....--...................-...-. +.....|\......\...........................-.................-.......\...............|............|........../.. +.....\...-.............|............\........\...../.-...........|.-.................\.........|.............. +......./......\..-............|..-...\......................\..........|....../..........|........./.......\.- +..-.....|........./|............./.....................|...|.\....-.......-..........|...\.\|-........\....|.. +......................../............-...........|...................../............-.......\.........-....... +......../..............-./...................................--........................\....-.......-..//..... +........|.....-....................|...../..............-..........-......|..\................................ +.......-..//.............../.....-......................./..|........./...........-..../..|.........-......... +.-...............-...........................\.......\...........-..................-.|...\..\.....\..\.....\. +.........-../............|......|.|.........................\.......-......\...\..............-../.../-\...... +.-...........-..|.....\/........-.|...................../.|........-...........\..........\.....\/|..../...\.\ +.-..........\.\-..\....-\/................./.\.......|.........................-............./....\........./. +.............\.........|............/...........\\................./..-..........................|//.......\.. +..../-........-.........................-\...........|................./..........|.............|.\..|........ +...-..|.....\..-..\.................\-.................|...../.-..........|........|/...-.-.\................. +.-.-............................|.....\..-..........|/|.........|....\............\.....\..-...........|/...|. +......................../...............|..-............................-.........|........../.-....../....... +.........|-...................|.....................-...........|.../..............................|....-...-. +..............|....................................................|.....|.......--.....\...\.............../. +..-...............|........|..................-............|.........../.....\................................ +........................|.........|............-....-..\../..................\..|......\.........\../.../..... +.|............................./..............-....-.......--....-.../..........|.....\.........-/.....|-./... +....-..............|...-.....\.....\/.-........................................-.\.............|...../|/...... +........./..-................/........./..............-............................................-....-..... +...........-........./...................../.........-........-................./.|........................... +............................................................-.............-..-......|..........-.\............ +....\/........................-../.../.................................\............/..........|./-......-...\ +....-............../..-.\.......|......-...|...|.........../......|../...........|..-.\.................../... +...-......../....-........................|...-............-.........................|/.....|.........|......- +...|........-.......-.......|...|\.......\............-...-...................................-|........-..... +....\/..-.....-.....-..--./-..-............../................|............\.............../.......|.......... +.........-.\.......-......|........-.............|..../..............\-.....\..........-..../................. +.................-......\|.--..../../.-......-.......-........\....-...../....................../............. +...../..../....../.............-......\..........|..........\..\........-.-................................... +..............................|........./........-..................|.............-.../.................\..-.. +.....|./......-........\...\.................|....-........|..............|................................... +\...|............................-..|.............../..............|.../......-......-.........../....../..... +..../.............................-..........\.....\-..................................-........../........... +..........................\....................-...-/.|....................../................................ +\.......|.........-............\\...............-...-...................../................................... +..\...................................-................................................../.......\.|.......... +...............................-................/........\....\.......|...................-.../.....|......... +...-..-............-.||..../././..........-\\............||...............-.\.........../...\........|...\...| +....\./.|./...................\..-.-./..-.....||............../............|.......\.|...../.................. +......-.......\.\.........\......|..\...\.-|.\..............|...|......\......./...\........................-. +........./.-......../....|..............................|...|...\........//./................................. +.........................|...........\...........|.....................\........//..-.................\-/..... +.\....................\..-.-..-..........|/.\..........\././.....................|............\....|...|/..... +......./.................../...-.................\.........../.........\................|....................| +......-..\....|..................\............\........|......|....../........................................ +.........\/..............|.............\.........-............-...........\.\..\./....../.....//.............. +.........../...\.......-.../.-.............\../....\|./............/...-..\........|..|/.....\......\\........ +..|............/..............................................-./...-.|..........-......../................... +...............-..|.......\.............\..../.....................|..........\...................\.....\./\.. +.|..........\..\...|......................\...........-.......--........................-..................... +...................../................\.|..........-..|................-................................-..... +...../.........|-............|.\.|.../\.....|../........\../.................................................. +................/........\../.....................\./..../...........-............\-......../../..\.|......... +..............|.........-......|/......././.....|..../.|...........-.....................|./.-.....\...|...... +.......|..-.......\..........|................................................/..\.....-..........-........... \ No newline at end of file diff --git a/resources/day16sample.txt b/resources/day16sample.txt new file mode 100644 index 0000000..c78b2e7 --- /dev/null +++ b/resources/day16sample.txt @@ -0,0 +1,10 @@ +.|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|.... \ No newline at end of file diff --git a/src/day16/part1.pas b/src/day16/part1.pas new file mode 100644 index 0000000..4de6d32 --- /dev/null +++ b/src/day16/part1.pas @@ -0,0 +1,146 @@ +{$mode objfpc} +{$RANGECHECKS ON} + +program day16part1; +uses sysutils; + +type + Tdirection = (Left, Up, Right, Down, NA); + +var + map: array[1..1000] of string; + illuminated: array[1..1000, 1..1000] of boolean; + splitted: array[1..1000, 1..1000] of boolean; + height, width: int32; + +procedure print_illuminated(); +var + i, j: int32; +begin + for i := 1 to height do + begin + for j := 1 to width do + case illuminated[i, j] of + true: write('#'); + false: write('.'); + end; + writeln(); + end; +end; + +function sum_illuminated(): int32; +var + i, j: int32; +begin + + sum_illuminated := 0; + + for i := 1 to height do + for j := 1 to width do + if illuminated[i, j] then + sum_illuminated := sum_illuminated + 1; +end; + +procedure step_illumination(x, y: int32; dir: Tdirection); +begin + + { range check } + if (x < 1) or (x > width) or (y < 1) or (y > height) then + exit; + + writeln('(', x, ',', y, ')'); + + illuminated[y, x] := true; + + { nested case my beloved } + case map[y, x] of + '.': + begin + { continue same direction } + case dir of + Up: step_illumination(x, y - 1, Up); + Left: step_illumination(x - 1, y, Left); + Down: step_illumination(x, y + 1, Down); + Right: step_illumination(x + 1, y, Right); + end; + end; + '/': + begin + { reflect direction } + case dir of + Up: step_illumination(x + 1, y, Right); + Left: step_illumination(x, y + 1, Down); + Down: step_illumination(x - 1, y, Left); + Right: step_illumination(x, y - 1, Up); + end; + end; + '\': + begin + { reflect direction but in the other way} + case dir of + Up: step_illumination(x - 1, y, Left); + Left: step_illumination(x, y - 1, Up); + Down: step_illumination(x + 1, y, Right); + Right: step_illumination(x, y + 1, Down); + end; + end; + '|': + begin + { up down passthrough, left right split } + case dir of + Up: step_illumination(x, y - 1, Up); + Down: step_illumination(x, y + 1, Down); + Left, Right: + begin + if not splitted[y, x] then // so we don't split forever in a loop + begin + splitted[y, x] := true; + step_illumination(x, y - 1, Up); + step_illumination(x, y + 1, Down); + end; + end; + end; + end; + '-': + begin + { lr passthrough, ud split } + case dir of + Left: step_illumination(x - 1, y, Left); + Right: step_illumination(x + 1, y, Right); + Up, Down: + begin + if not splitted[y, x] then // so we don't split forever in a loop + begin + splitted[y, x] := true; + step_illumination(x - 1, y, Left); + step_illumination(x + 1, y, Right); + end; + end; + end; + end; + end; +end; + +var + + file_: text; + +begin + + assign(file_, 'resources/day16.txt'); + reset(file_); + + height := 0; + + while not eof(file_) do + begin + height := height + 1; + readln(file_, map[height]); + end; + width := length(map[1]); + + step_illumination(1, 1, Right); + print_illuminated(); + writeln('Total illuminated is ', sum_illuminated()); + +end. diff --git a/src/day16/part2.pas b/src/day16/part2.pas new file mode 100644 index 0000000..fdd6e89 --- /dev/null +++ b/src/day16/part2.pas @@ -0,0 +1,219 @@ +{$mode objfpc} +{$RANGECHECKS ON} + +program day16part1; +uses sysutils; + +type + Tdirection = (Left, Up, Right, Down, NA); + +var + map: array[1..1000] of string; + illuminated: array[1..1000, 1..1000] of boolean; + splitted: array[1..1000, 1..1000] of boolean; + height, width: int32; + +procedure print_illuminated(); +var + i, j: int32; +begin + for i := 1 to height do + begin + for j := 1 to width do + case illuminated[i, j] of + true: write('#'); + false: write('.'); + end; + writeln(); + end; +end; + +function sum_illuminated(): int32; +var + i, j: int32; +begin + + sum_illuminated := 0; + + for i := 1 to height do + for j := 1 to width do + if illuminated[i, j] then + sum_illuminated := sum_illuminated + 1; +end; + +procedure reset_illuminated_splitted(); +var + i, j: int32; +begin + for i := 1 to height do + for j := 1 to width do + begin + illuminated[j, i] := false; + splitted[j, i] := false; + end; +end; + +procedure step_illumination(x, y: int32; dir: Tdirection); +begin + + { range check } + if (x < 1) or (x > width) or (y < 1) or (y > height) then + exit; + + illuminated[y, x] := true; + + { nested case my beloved } + case map[y, x] of + '.': + begin + { continue same direction } + case dir of + Up: step_illumination(x, y - 1, Up); + Left: step_illumination(x - 1, y, Left); + Down: step_illumination(x, y + 1, Down); + Right: step_illumination(x + 1, y, Right); + end; + end; + '/': + begin + { reflect direction } + case dir of + Up: step_illumination(x + 1, y, Right); + Left: step_illumination(x, y + 1, Down); + Down: step_illumination(x - 1, y, Left); + Right: step_illumination(x, y - 1, Up); + end; + end; + '\': + begin + { reflect direction but in the other way} + case dir of + Up: step_illumination(x - 1, y, Left); + Left: step_illumination(x, y - 1, Up); + Down: step_illumination(x + 1, y, Right); + Right: step_illumination(x, y + 1, Down); + end; + end; + '|': + begin + { up down passthrough, left right split } + case dir of + Up: step_illumination(x, y - 1, Up); + Down: step_illumination(x, y + 1, Down); + Left, Right: + begin + if not splitted[y, x] then // so we don't split forever in a loop + begin + splitted[y, x] := true; + step_illumination(x, y - 1, Up); + step_illumination(x, y + 1, Down); + end; + end; + end; + end; + '-': + begin + { lr passthrough, ud split } + case dir of + Left: step_illumination(x - 1, y, Left); + Right: step_illumination(x + 1, y, Right); + Up, Down: + begin + if not splitted[y, x] then // so we don't split forever in a loop + begin + splitted[y, x] := true; + step_illumination(x - 1, y, Left); + step_illumination(x + 1, y, Right); + end; + end; + end; + end; + end; +end; + +var + + file_: text; + + max: int32; + i, t: int32; + + // for pretty printing + max_dir: Tdirection; + max_idx: int32; + +begin + + assign(file_, 'resources/day16.txt'); + reset(file_); + + height := 0; + + while not eof(file_) do + begin + height := height + 1; + readln(file_, map[height]); + end; + width := length(map[1]); + + max := -1; + max_dir := NA; + max_idx := -1; + + { check up + down } + for i := 1 to width do + begin + step_illumination(i, 1, Down); + t := sum_illuminated(); + reset_illuminated_splitted(); + + if t > max then + begin + max := t; + max_dir := Down; + max_idx := i; + end; + + step_illumination(i, height, Up); + t := sum_illuminated(); + reset_illuminated_splitted(); + + if t > max then + begin + max := t; + max_dir := Up; + max_idx := i; + end; + end; + + { check left + right } + for i := 1 to height do + begin + step_illumination(1, i, Right); + t := sum_illuminated(); + reset_illuminated_splitted(); + + if t > max then + begin + max := t; + max_dir := Right; + max_idx := i; + end; + + step_illumination(width, i, Left); + t := sum_illuminated(); + reset_illuminated_splitted(); + + if t > max then + begin + max := t; + max_dir := Left; + max_idx := i; + end; + end; + + writeln('The best configuration is going ', max_dir, ' at index ', max_idx); + writeln('The max illumination is ', max); + + +end.