aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOrfeas <38209077+0xfea5@users.noreply.github.com>2023-12-03 09:43:22 +0200
committerOrfeas <38209077+0xfea5@users.noreply.github.com>2025-10-28 23:20:45 +0200
commit6c6abed6cb4f0b6de4fcd065af38027d86fc82c0 (patch)
treea884a38300ccc95bb8412b7c3020b8418ca80cf4
parentday2 + example files (diff)
downloadaoc23-6c6abed6cb4f0b6de4fcd065af38027d86fc82c0.tar.gz
aoc23-6c6abed6cb4f0b6de4fcd065af38027d86fc82c0.zip
day3
-rw-r--r--day03/example.txt10
-rw-r--r--day03/input.txt140
-rw-r--r--day03/solution.zig138
-rwxr-xr-xinit.sh20
4 files changed, 304 insertions, 4 deletions
diff --git a/day03/example.txt b/day03/example.txt
new file mode 100644
index 0000000..b20187f
--- /dev/null
+++ b/day03/example.txt
@@ -0,0 +1,10 @@
1467..114..
2...*......
3..35..633.
4......#...
5617*......
6.....+.58.
7..592.....
8......755.
9...$.*....
10.664.598..
diff --git a/day03/input.txt b/day03/input.txt
new file mode 100644
index 0000000..47a3e47
--- /dev/null
+++ b/day03/input.txt
@@ -0,0 +1,140 @@
1..224.....487...................718.....................378............................................284........310......313..........311.
2....*..............................*744....486*485......*......741......@...359.#666...439................*925....*......$..+........@......
3.235................758..440...........................251....*......262.....*..........*......................752......774.......515.......
4.........705%..@746........+..942*591.347.470...#..257.........637...........793.......299..../.....813....509......464......&.........688..
5.....82................................*.../..901.....*..................836.....&............814...*........*..............80...17*....*...
6.../...*...679.661.....299...........222.............875.....213...161............964...894.........998.....310....258.85...........735.586.
7.650..23..#......*.......................................760*........@./........................202...................*.....339.............
8..............598......#.....536....702*.........705..........793......957............./........*...935...........965.......................
9..................*.357...+.....@.......242.......*......283..*...=956.......118.......959...184...*..............*....401*527.....348...161
10.63=...955.523..77......978.846....849..........699..........790........=911.*.....87...............486.........914..............=..........
11.........*.........842......*.......*..................*127....................844...*....946.......................235...209@...710.735....
12...........@.........*.....144....226....298........560.....253.........431......*...586.......723..../...482.......&...................*...
13...........871../918........................+....................329*......%.....906.............*...350.....@...............365.......361..
14......................536............................+..793..........664.....739................507......289.....256%...73..*...............
15.356..834..............*.........96.939..............28.+....=..............*...........................*................$.553..........526.
16...*...*.............501..746....*..-.......=.................890.....+...701..............807........447......97$..%...........58......$...
17.891....346...699...........*.421......+....666......198*791......340.433............#...%...*.......................316........*.....%.....
18..............%........81.984...........462....................../..........116...711....963...$................380.......745...85....224...
19.........370......923..@.....................563...........188+.........326*...................67.815..............*582.....................
20.........*........-........#530.........489........59...................................................#.....57.........100.*.......441*161
21.637*....961...........118...........21*...........@..216+.........................+....432*.*309..64....992.......81......=.781.644........
22.....324.........126......*166...............&...................385*..............233............*................*..436...........*517....
23.88*.........390*................355..335...372....+254..............294...$589........&..803/.....38..-...........56..$....................
24....235.-.........+196...948....*........-................359.........................398.............608..../.................145..346.....
25........915.................-.485.945.................936*................114.............118...............490.536..............*.%........
26.663........61.......856..........+....343....859...............*345.......*...=862............711.............................313.....606..
27........23...$.....@..................*......................316.....529.362.............49......................307*791....................
28225...............281...#..222.....@..283..............675............*.........911*90..-.....914..%39....509...............................
29.......797*....$........23..../...895.............67.......164.896.....554.....................*............=..............460........181...
30...........211.474...............................@...........*....*201..........911.717.#....192.75..679...........$.........*.........=....
31....355.........................&.....................678.650......................*.....128.....*.....*.........925..230...105.............
32....*.......409#.......*897.....698....427.........+....*.......*...............................313...670..521.......*....&.............*308
33...838..335.................367........-..........325....233...224...950......688.....199.774.........................793.108........501....
34...........*..102..924.349...+..595....................................*................&..............395+.....................753.........
35.........318...*......*........*....676...759.154...................524...........908.........694.211................173...........*214..424
36....458........403...........958.98....*........*..............688......957...488..+...........*.../....................................*...
37.......*.............591...............218.....203.....196$.......+....*........*......................765......971......./....927*741..227.
38......592..%60.$.....*.....885/....287...............................474.........409......226.174.......@........*.....230..................
39...............162.169.................510..962..............17......................32@..*...*.................225....................-....
40.....880...................201.....823*........*...914.724.........89...&.......267=.....717.54.436..........60...............&.........69..
41710........601....306.287.....+.........65...308....&...=.../......*.....926..................................*........278.415......-.......
42..............*..*......&..........241.%...................827../.265.............662.678......@...#......@.....$......-............734.144.
43.............424..434.....701.....*........380*731.............24.................*...&........973..741....728..357.........................
44........304.................*...881................238.....740.........947........587...22...........................271*........603....@...
45...................266.....918....................*..........+.519.617..*...826..........*............./268..974.........486.376....*.295...
46...939.........363.*...............378..........302....586........*....740...=..........380........931...........................12.........
47...*..........-.....387...............*..588..............*..../...............315...................-...712..491.........956.....#.........
48.180.454...................104..661...91...*.............811....370......471...-....419....804..........*...................*...............
49.....*.......@../464........%..*............243.....578...................&..........*...........604..250.....784....750....998.............
50.....245....165.................771...................&.......#.691...768....654....261.........................*.......*............566....
51441..................91.....194.......643...932*............379.#....$.......*.................86.............940..263.638...........*......
52...+.............%.../.......#.........*........344...795..................991..........544....*..................*..........767...266......
53.......-..708..727.....191$......*.....698....=...................990................+.....*....308.......@......325.........*.........44...
54...18.397....=.....93.........649.900.........62....-........322.....*.......@......339....619..........404..........540........./....*.....
55..../...............%............................742............*...........196................874.205........96#..............203.224......
56.........704...727.................496.......*........135....769........./.................%....&....*.../..=.........757...................
57........*........*..424..........&........714.5........*...........@....884...............114.......674.144..83..........*302...........%...
58........29....179..*............104..................531....629.455..................473......300....................254..............640...
59..................296.......................................................1........*.......%..............548........*.....103............
60....381...........................365#..713.282.......%744....................../....709...#...=...431...$.*......*.....837.....*9.....*....
61......*.90.....347.179#...................%.......359..........92............304...*.....923.446.......385.699.....749..............193.206.
62....922.+.......*.......-197.....................*.......128........273....@.....758..............439...................650*54.736#.........
63................817..-..........152..594..949..807.......$...224...*.....124............889.......=...................................625...
64....139....667......239........*...........*.......387........%..806........../...........*..788....632...........=8......584-......*.%.....
65...........*.................280...........11.........*726............181......761..-....66.....%....-................64...........14.......
66............742.736.725..........708.667..........332.......828*750......-.........425.......................944.....*...................926
67.719...110...........*.....348...=..........121......*........................................*.......961.....*.......971...........+.......
68..../.....*629........830..*............91....&..241..491...930............561................889....*....710.333..............163...311....
69......%...................252.../..................*...........*270.......................165.....208.......*..................../.......694
70...914...126...267.183........952..$645......414.822................................69.......-..........=..112.464..267....359..........*...
71.............7*..........................806.../.....931.....*.....355..............*....928...........854.........-..............*993...101
72439.897...........%...........8......381...*........*....748..987....*............437.......*....954.......................*................
73.......*595....783........49.....110.$...675...8...60.....=........981........&..........730....*.............336.......120.790..........386
74............90..............*212...*..........*.......985.....153........*..12...912*383......665.....754.....-.....................950.....
75......790#.*........896@.........773....777..581..514*....910.....5.....579.............................=.......#.....................*.....
76...&.......496...........347..............*...............*...................../............................790..............334......912..
77692............609....&.......694..........693...213....620......695......871..385.......82..........785.......................*..163.......
78.........#....*......984.........*.................=............*.......................................*.545...-............978.....*......
79......971...477.............465......196*313..........447.......766......49.......126*480.............798......792..359*193...........837...
80................157...........+..................773.=..............752$....................447*565..........................*53............
81.384%..........@.........449............470..749...@.........@792..............138..527.......................339.....299.505...........287.
82......691........700........@......96..........*........721*.....................@....*...942......845........*......-.........713..........
83.....*......260/...*...........................491..452.....336.......................73....+..960..........238...........163$..&...=.@.....
84....689.............831...282....709.960.............=.............204............................#..............59...97..........199..111..
85........................$........../........-....498......................557....&...........972............450.............................
86.............653........476...398.........619...*.....*280..923..............*31..691.........*........=...*.....67......782................
87..............*....%..........*.........+......268...........&........428....................408....740....576....*.......*.........$.......
88....-../...734......317.114.332...795...743.........................-..=..908...........381.......................914...420.*647....528.....
89..284.825...............*......................721/.=929.........324.....*....167.561...*.....815*466.......................................
90..............389..304.480.......851..@....585..............942%......#.496....*....-.53................170....938*..........518............
91........368..*.....@..............*....941..+.......*...............930.........364.......522......................59.........+....$....652.
92...........+.963...................949...........828.784......860............*......420.....*..................*......548...........310.*...
93.....476...............*...............=...94.+..................&........589.833....*..426.940.....&.....959.993.&88....................398
94.......*................624....917...838..*...664.......721........................833...$.......672......$............19...................
95....350.......................&....=..............333.................707....................................240..336....=...746.263..$463..
96.........................-.........220.....663...../.............*849..#..631.....712....=.../...308.580.............*..........*...........
97.78@......../..........422....444.............*...............281............*...*....447..37.......*................814....................
98........./...788...810.........&..............369..985....-................92................................684...............74...........
99......279.........*......128/....464........#...../......944........978...............163..984*5.@............+.......165......*....285.....
100..................889.............*.......969........................%.......5......&....$........272....................*......374.........
101.........&..............@948..*..13.358........796.............632............*....789........91*..............*66........152...............
102........508...+..............3...........544.....$.517.....703....*774.......434............/....794........932.............................
103...$...........241.....74...........974..*...954............*.......................375..772...........218.........414...........*366....252
104.501..566...............*..........*....599.....#........256.....468@..942.101.........*..........156...*.............*.......998...........
105...../.............263.312....+..767..............#.........................*...&95..838.162=.....+....130.........583..544.......176.......
106............................936......826.......421.......-139.......982...893.......................*..................*........./..........
107.284.....=724...................661.$....694........85..........438.............51...............616.698.*36.....262......357+........224...
108....@..........491........+........................@......193......*596...........%../844...387.................$.....132.........%.........
109.......84..............362.................................*..98..........&................*....14....394..........$..............56........
110.........*432.....512................704......424.............*............74........196%..875...*.....*............132.......229...........
111..............723*.........568........*..108........672......55.172...217.........................289.427........................*..........
112.........#795.........214....*.336...94....*....988................%..&.............*86......*...............838&.......540.......902.......
113.922...........288........719.....*........845...&..558......#...................524......195.704.....................................77....
114...*....842....../...166........883...-................*503..451..305../....600............................900..280.....*169...586..........
115751....*.........../.*......944......116....416.........................877....*....418...194........644..*....=.....244..........#.....354.
116.......118......208...................................788...................826........$....$...207.....@..152...........893*608.......&....
117..948.......................203../....206.............*....341./977.916.........49...#........@....-............+385........................
118...*......266.................*...607...@.990......964..............-..........*.....340...697........62..............526.....-.............
119..675.....*..........612....604.............*..............492.884...........212...............*157....*...679.........*....706...716%.518..
120......103.749..989...*...................739..............$......=..=..................212..684.....35.......*.........785..............+...
121..343*......../.......680.....................*423.................476....&....420..............241..*......532.....+.......864$.....&......
122..................826........403..311...60.451.............+...........562....../.......306.......*..............420..................321...
123...241..............*...........+..=....@......*240.......845....@.........740.................968..389.177*...................640..........
124......*82...........963...211%...............38...............901.....404.........&..253..............-.....37.........218.501...*....320...
125....*...........................#215...264......298........................*970..801....*....569...............3.......*......%..859.*......
126.136.818...212...........91*72........*.........-...750.....775*728.29.270...............984.&...371..........*.........639..........670....
127...........*.../959................438.....524......*...943.........*................755...................162.................446#.........
128..........290.............751................*.....557..*...483.............421.........*............189.%................/.................
129....................9.786..-......835.......978........445.%..........350........860..662.......#...*....354........*.....682...............
130.117..57+....441...*....+............................................../............*.........88....142..........%.927......................
131...*...........*..402....................874.....734.926$../...............*633......219.84/............662...205...................334.....
132...981........367........338...332/.........=......*......119....375#.334.......146..............%.....$................384.............-511
133........165................=....................666.....................#..614.....-..%744..617.887.......848*..............................
134...........*.203.593...158....*..........152..............+817.....866.......&...............*......../.......904........701.165.80.........
135404..977.93..*....*....*...396.281......*....953....*................*.........116..97.70=..179....107..338................$...*....622.....
136......=.....445........................382.....*...672................859......*...%....................&..........-.571......717....*......
137..................240............175..........808..............225..........988............/604..............232.448..*..651......769.......
138.........569*....*........975*.....*....968..............585.....*................26.................................394....@.142...........
139......*......498..969.........360.666...%.........................919.......360........-.*.........%...................................484..
140...407................886...................................84......................933...101....58........839..425.........................
diff --git a/day03/solution.zig b/day03/solution.zig
new file mode 100644
index 0000000..b98260a
--- /dev/null
+++ b/day03/solution.zig
@@ -0,0 +1,138 @@
1const std = @import("std");
2const print = std.debug.print;
3const assert = std.debug.assert;
4const ArrayList = std.ArrayList;
5const mem = std.mem;
6const isDigit = std.ascii.isDigit;
7const Self = @This();
8
9const fin = mem.trim(u8, @embedFile("./input.txt"), &std.ascii.whitespace);
10var gpa = std.heap.GeneralPurposeAllocator(.{}){};
11const allocator = gpa.allocator();
12const MapOfNumDesc = std.HashMap(NumDesc, void, NumDescCtx, 80);
13
14const NumDesc = struct {
15 i: u64,
16 left: u64,
17 right: u64,
18};
19
20const NumDescCtx = struct {
21 pub fn hash(self: @This(), K: NumDesc) u64 {
22 _ = self;
23 return K.left | (K.right << 32);
24 }
25
26 pub fn eql(self: @This(), K1: NumDesc, K2: NumDesc) bool {
27 _ = self;
28 // Checking .i and .left can uniquely identify the number in the grid
29 return K1.i == K2.i and K1.left == K2.left;
30 }
31};
32
33fn isSymbol(c: u8) bool {
34 return c != '.' and !std.ascii.isDigit(c);
35}
36
37fn isGear(c: u8) bool {
38 return c == '*';
39}
40
41fn getNumberAt(map: ArrayList([]const u8), i: u64, j: u64) NumDesc {
42 var left: u64 = j;
43 var right: u64 = j;
44
45 // Lookahead checking to avoid dropping below 0
46 while (left > 0 and
47 isDigit(map.items[i][left - 1])) : (left -= 1)
48 {}
49
50 while (right < map.items.len and
51 isDigit(map.items[i][right])) : (right += 1)
52 {}
53
54 return NumDesc{
55 .i = i,
56 .left = left,
57 .right = right,
58 };
59}
60
61// Evaluated numbers are managed by the caller for more flexibility.
62// P1: Each number is only evaluated once
63// P2: Each number may be evaluated an arbitrary number of times
64fn findAdjacent(map: ArrayList([]const u8), i: u64, j: u64, evaluated: *MapOfNumDesc) ArrayList(u64) {
65 var surroundNums = ArrayList(u64).init(allocator);
66
67 for (@max(i - 1, 0)..@min(i + 2, map.items.len)) |ai| {
68 for (@max(j - 1, 0)..@min(j + 2, map.items[0].len)) |aj| {
69 if (!isDigit(map.items[ai][aj])) {
70 continue;
71 }
72 const p = getNumberAt(map, ai, aj);
73 const s = map.items[p.i][p.left..p.right];
74 const n = std.fmt.parseInt(u64, s, 10) catch unreachable;
75
76 if (evaluated.contains(p)) {
77 continue;
78 }
79 evaluated.put(p, undefined) catch unreachable;
80 surroundNums.append(n) catch unreachable;
81 }
82 }
83
84 return surroundNums;
85}
86
87fn part1(map: ArrayList([]const u8)) void {
88 var ans: u64 = 0;
89 var evaluated = MapOfNumDesc.init(allocator);
90 defer evaluated.deinit();
91
92 for (map.items, 0..) |line, i| {
93 for (line, 0..) |c, j| {
94 if (isSymbol(c)) {
95 const adj = findAdjacent(map, i, j, &evaluated);
96
97 for (adj.items) |adj_num| {
98 ans += adj_num;
99 }
100 }
101 }
102 }
103
104 print("{d}\n", .{ans});
105}
106
107fn part2(map: ArrayList([]const u8)) void {
108 var ans: u64 = 0;
109 var evaluated = MapOfNumDesc.init(allocator);
110 defer evaluated.deinit();
111
112 for (map.items, 0..) |line, i| {
113 for (line, 0..) |c, j| {
114 if (isGear(c)) {
115 const adj = findAdjacent(map, i, j, &evaluated);
116 evaluated.clearRetainingCapacity();
117
118 if (adj.items.len == 2) {
119 ans += adj.items[0] * adj.items[1];
120 }
121 }
122 }
123 }
124
125 print("{d}\n", .{ans});
126}
127
128pub fn main() !void {
129 var splitLines = mem.splitScalar(u8, fin, '\n');
130 var lines = ArrayList([]const u8).init(allocator);
131
132 while (splitLines.next()) |line| {
133 try lines.append(line);
134 }
135
136 part1(lines);
137 part2(lines);
138}
diff --git a/init.sh b/init.sh
index 5239b81..dacf6cb 100755
--- a/init.sh
+++ b/init.sh
@@ -18,11 +18,23 @@ echo \
18 'const std = @import("std"); 18 'const std = @import("std");
19const print = std.debug.print; 19const print = std.debug.print;
20const assert = std.debug.assert; 20const assert = std.debug.assert;
21const ArrayList = std.ArrayList;
21const mem = std.mem; 22const mem = std.mem;
22const input = @embedFile("./input.txt"); 23
24const fin = mem.trim(u8, @embedFile("./input.txt"), &std.ascii.whitespace);
25var gpa = std.heap.GeneralPurposeAllocator(.{}){};
26const allocator = gpa.allocator();
27
28pub fn part1() void {
29
30}
31
32pub fn part2() void {
33
34}
23 35
24pub fn main() !void { 36pub fn main() !void {
25 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 37
26 defer _ = gpa.deinit(); 38 part1();
27 var alloc = gpa.allocator(); 39 part2();
28}' >$DIR/solution.zig 40}' >$DIR/solution.zig